package com.mfp.java.testYou.ui.tags;

import java.io.IOException;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.log4j.Logger;

import com.mfp.java.testYou.core.I18n;
import com.mfp.java.testYou.ui.UIHelper;

/**
 * Custom tag for i18n support.<br/>
 * An overhead over the I18n class.
 */
public class I18nTag extends BodyTagSupport {

	private static final Logger log = Logger.getLogger(I18nTag.class);

	private static final long serialVersionUID = 2898730075738891024L;

	@Override
	public int doAfterBody() {
		String content = this.bodyContent.getString();
		JspWriter out = bodyContent.getEnclosingWriter();
		try {
			String lang = UIHelper.getLang(this.pageContext.getRequest());
			String value = I18n.getInstance().getMessage(lang, content.trim());
			out.print(value);
		} catch (IOException e) {
			log.error(e.getClass().getName() + " " + e.getMessage());
		}
		return BodyTagSupport.EVAL_PAGE;
	}
}
